package com.example.goukakuproject;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.Toast;

public class GKQuizsettingtab extends Activity {

	int bai = 0, trinhDo = 0;
	int tuBai = 0;
	int denBai = 0;
	int soCauHoi = 0;
	int loai = 0; //loai 1: Tu Vung, loai 2: Chu Han
	int loai2 = 0; // 1: Nhat Viet va Kanji Nghia. 2 nguoc lai
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_gkquizsettingtab);
		Intent callerIntent=getIntent();
		Bundle packageFromCaller = callerIntent.getBundleExtra("QuizIntent");
		bai = packageFromCaller.getInt("bai");
		trinhDo = packageFromCaller.getInt("trinhdo");
		Toast.makeText(this, "Trình độ N" + trinhDo + ". Bài " + bai, Toast.LENGTH_SHORT).show();
		final Spinner spinLoai = (Spinner) findViewById( R.id.spinnerLoai);
		final Spinner spinTuBai = (Spinner) findViewById( R.id.spinerTubai);
		final Spinner spinDenBai = (Spinner) findViewById( R.id.spinnerDenbai);
		final Spinner spinSoCauHoi = (Spinner) findViewById( R.id.spinnerSocauhoi);
		final RadioButton radio1 = (RadioButton) findViewById(R.id.radio1InQuiz);
		final RadioButton radio2 = (RadioButton) findViewById(R.id.radio2InQuiz);
		final Button btnStart = (Button) findViewById(R.id.btnStartInQuiz);
		String[] arr = {"Tu vung","Chu Han"};
		setSpinner(arr, spinLoai);
		final String[] arr1 = {"1","2","3","4","5","6","7","8","9","10"};
		setSpinner(arr1, spinTuBai);
		final String[] arr2 = {"1","2","3","4","5","6","7","8","9","10"};
		setSpinner(arr2, spinDenBai);
		final String[] arr3 = {"1","2","3","4","5","6","7","8","9","10"};
		setSpinner(arr3, spinSoCauHoi);
		spinLoai.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				if (arg2 == 0){
					radio1.setText("Nhat - Viet");
					radio2.setText("Viet - Nhat");
					loai = 1;
				}			
				else{
					radio1.setText("Chu Han - Nghia");
					radio2.setText("Nghia - Chu Han");
					loai = 2;
				}
				
			}
			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}			
			
		});
		spinTuBai.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {				
				tuBai = Integer.parseInt(arr1[arg2]);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		spinDenBai.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {				
				denBai = Integer.parseInt(arr2[arg2]);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		spinSoCauHoi.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {				
				soCauHoi = Integer.parseInt(arr3[arg2]);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		radio1.setOnClickListener(new View.OnClickListener() {			
			@Override
			public void onClick(View arg0) {			
				loai2 = 1;
			}
		});
		radio2.setOnClickListener(new View.OnClickListener() {			
			@Override
			public void onClick(View arg0) {			
				loai2 = 2;
			}
		});
		btnStart.setOnClickListener(new View.OnClickListener() {			
			@Override
			public void onClick(View arg0) {
				 Intent myIntent=new Intent(GKQuizsettingtab.this,GKQuizscreen.class);		 
				 Bundle bundle=new Bundle();
				 bundle.putInt("trinhdo", trinhDo);
				 bundle.putInt("tubai", tuBai);
				 bundle.putInt("denbai", denBai);
				 bundle.putInt("socauhoi", soCauHoi);
				 bundle.putInt("loai", loai);
				 bundle.putInt("loai2", loai2);
				 myIntent.putExtra("QuizTabIntent", bundle);
				 startActivity(myIntent);
				
			}
		});
	}
	private void setSpinner(String[] arr,Spinner spin){				
		//Gán Data source (arr) vào Adapter
		ArrayAdapter<String> adapter=new ArrayAdapter<String>
		(
		 this,
		 android.R.layout.simple_spinner_item,
		 arr
		);
		//phải gọi lệnh này để hiển thị danh sách cho Spinner
		adapter.setDropDownViewResource
			(android.R.layout.simple_list_item_single_choice);
		//Thiết lập adapter cho Spinner
		spin.setAdapter(adapter);
		//thiết lập sự kiện chọn phần tử cho Spinner		
	}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.gkquizsettingtab, menu);
		return true;
	}

}
